<template>
  <div class="LabelChild">
    <el-card>
      <h1>
        <i
          style="font-weight: 700; margin-right: 10px"
          class="el-icon-arrow-left"
          @click="back"
        ></i
        >{{ faName }}
      </h1>
      <el-row :gutter="30">
        <el-col :span="4" v-for="(item, index) in list" :key="index">
          <div
            class="card"
            :style="'background-color:' + item.color"
            @click="clickCard(item)"
          >
            {{ item.label_name }}
            <i class="el-icon-close" @click.stop="close(item)"></i>
          </div>
        </el-col>
        <el-col :span="4" v-if="list.length < 30">
          <div class="addcard" @click="add">
            <i class="el-icon-plus"></i>
            <br />
            <span>{{ "(" + list.length + "/30)" }}</span>
          </div>
        </el-col>
      </el-row>
    </el-card>
    <el-dialog
      title="添加二级标签"
      :visible.sync="centerDialogVisible"
      width="30%"
      center
      @close="closeLabel"
    >
      <h3>当前一级标签：{{ faName }}</h3>
      标签名：
      <br />
      <br />
      <el-input maxlength="6" show-word-limit v-model="label_name"></el-input>
      <br />
      <br />
      颜色：
      <br />
      <br />
      <div class="color">
        <el-input placeholder="请输入内容" v-model="color" style="width: 80%">
        </el-input>
        <el-color-picker v-model="color"></el-color-picker>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  labelList,
  labelAdd,
  labelSetLabelStatus,
} from "@/api/operationManagement";
export default {
  data() {
    return {
      centerDialogVisible: false,
      list: [],
      pid: "",
      faName: "",
      label_name: "",
      color: "",
    };
  },
  methods: {
    clickCard(obj) {
      console.log(obj);
    },
    closeLabel() {
      this.label_name = "";
      this.color = "";
    },
    add() {
      this.centerDialogVisible = true;
    },
    back() {
      this.$router.push("/OperationManagement/LabelManagement");
    },
    async getList() {
      const res = await labelList({
        pid: this.pid,
      });
      // console.log();
      if (res.code == 0) {
        this.list = res.data;
      }
    },
    async submit() {
      const res = await labelAdd({
        name: this.label_name,
        pid: this.pid,
        color: this.color,
      });
      if (res.code == 0) {
        this.$message({
          type: "success",
          message: "添加成功！",
        });
        this.getList();
        this.label_name = "";
        this.centerDialogVisible = false;
      }
      // console.log(res);
    },
    close(obj) {
      console.log(obj);
      this.$confirm("是否要删除此卡片?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const res = await labelSetLabelStatus({
            id: obj.id,
            status: "10", // 1.使用 5.禁用 10.删除
          });
          if (res.code == 0) {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.getList();
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消",
          });
        });
    },
  },
  created() {
    if (this.$route.query) {
      this.pid = this.$route.query.pid;
      this.faName = this.$route.query.name;
    }
    this.getList();
  },
};
</script>

<style lang="less" scoped>
.LabelChild {
  padding: 20px;
  .card {
    height: 100px;
    line-height: 100px;
    font-size: 24px;
    box-shadow: 0px 0px 10px #ccc;
    border-radius: 5px;
    background-color: #fdfcf8;
    text-align: center;
    margin-bottom: 30px;
    position: relative;
    .el-icon-close {
      position: absolute;
      font-weight: 700;
      right: -10px;
      top: -10px;
    }
  }
  .addcard {
    height: 100px;
    font-size: 24px;
    padding: 25px;
    box-shadow: 0px 0px 10px #ccc;
    border-radius: 5px;
    // background-color: #fdfcf8;
    text-align: center;
    margin-bottom: 30px;
    color: #ccc;
  }
  .color {
    display: flex;
    align-items: center;
  }
}
</style>